#include <pthread.h>
#include "sys.h"
#include "nstar_hcf4051.h"

static pthread_mutex_t ch_mutex = PTHREAD_MUTEX_INITIALIZER;
#define OS_MUTEX_LOCK()     pthread_mutex_lock(&ch_mutex)
#define OS_MUTEX_UNLOCK()   pthread_mutex_unlock(&ch_mutex)

#define GpioSet_4051A(n) do{}while(0)
#define GpioSet_4051B(n) do{}while(0)
#define GpioSet_4051C(n) do{}while(0)
#define GpioSet_4051INH(n) do{}while(0)
#define GpioSet_AMP_STANDBY(n) do{}while(0)

static volatile enum NSTAR_HCF4051  board_sel_sta = HCF_CHANLCLOSE;
static struct board_ch_level h_board_ch;
static volatile unsigned char board_sel_eflag = 0;
static unsigned char dev_noinit=1;

void board_ch_init(void)
{
    unsigned char i=0;
    h_board_ch.validCount=  CH_NB_MAX;
    h_board_ch.level[i++].id= NSTAR_HCF4051_INTERPHONE;
    h_board_ch.level[i++].id= NSTAR_HCF4051_DVB;
    h_board_ch.level[i++].id= NSTAR_HCF4051_MP3;
    h_board_ch.level[i++].id= NSTAR_HCF4051_MIC;
    h_board_ch.level[i++].id= NSTAR_HCF4051_LINE1;
    h_board_ch.level[i++].id= NSTAR_HCF4051_LINE2;
    dev_noinit=0;
}


//bit 2 1 0
//io  C B A
static void board_ch_set(unsigned char channel)
{
	if(channel < HCF_CHANLCLOSE){
		GpioSet_4051A(channel&0x1);
		GpioSet_4051A(channel&0x2);
		GpioSet_4051A(channel&0x4);	
        GpioSet_4051INH(0);    
	    GpioSet_AMP_STANDBY(1);
	}  
	else{
		GpioSet_4051INH(1);
        GpioSet_AMP_STANDBY(0);
	}
}


static unsigned char update_trumpet_status(enum NSTAR_HCF4051 channel, unsigned char status, unsigned char lv)
{
	unsigned char index = 0;
    //OS_MUTEX_LOCK();
	for(index = 0; index < h_board_ch.validCount;index++ )
	{
		if(h_board_ch.level[index].id == channel )
		{
            h_board_ch.level[index].status = status;
			if(status != 0){
                h_board_ch.level[index].lv = lv;
            }
            else{
                h_board_ch.level[index].lv = 0;
            }
			break;
		}
	}
    //OS_MUTEX_LOCK();
	return index < CH_NB_MAX ? 1 : 0;
}

static unsigned char ch_sel(void)
{
	unsigned char current_sel= CH_NB_MAX, current_eflag= 0, i;
    unsigned char ret=0;
    //OS_MUTEX_LOCK();

    /*应急的优先级高*/
	for(i = 0; i < h_board_ch.validCount;i++ ){
		if(h_board_ch.level[i].lv == 1 && h_board_ch.level[i].status == CHANNEL_OPEN){
			current_sel = i;
            current_eflag= 1;
			break;
		}
	}

    /*不存在应急，计算普通的优先级*/
    if(current_sel == CH_NB_MAX){
        for(i = 0; i < h_board_ch.validCount;i++ ){
    		if(h_board_ch.level[i].status == CHANNEL_OPEN){
    			current_sel = i;
    			break;
    		}
        }
    }


    if(board_sel_sta != current_sel || board_sel_eflag != current_eflag){
        ret=1;
    }
    
	if(current_sel == CH_NB_MAX){
		board_ch_set(HCF_CHANLCLOSE);
        if(ret) {
            //tmp_middle_vol_ctl(0);
        }
	}
	else{
		board_ch_set(h_board_ch.level[current_sel].id);
        if(ret) {
            //tmp_middle_vol_ctl(current_eflag);
        }
	}
    
   
    board_sel_sta= current_sel;
    board_sel_eflag= current_eflag;

	//OS_MUTEX_LOCK();
    return ret;
}



unsigned char visit_ch_sel(void)
{
	unsigned char sel= board_sel_sta;
	if(sel > CH_NB_MAX)
		sel= CH_NB_MAX;
	return sel;
}

unsigned char visit_ch_eflag(void)
{
    unsigned char ret=0;
    if(board_sel_sta != CH_NB_MAX){
        ret= 1+ board_sel_eflag;
    }
	return ret;
}

extern void nstar_ts_pack_play(unsigned char emflg);
extern void nstar_ts_pack_play_stop(void);
extern void adp_pgNet_start(void);
extern void adp_pgNet_stop(void);
extern void nstar_front_set_updateflag(void);

static void encoder_work(void)
{
    printf("board ch change: %d, lv %d\n", board_sel_sta, board_sel_eflag);	
    //nstar_front_set_updateflag();

/*    

*/
    //if(board_sel_sta != CH_NB_MAX && board_sel_sta != CH1_NB_DVB){
    if(board_sel_sta != CH_NB_MAX){
        nstar_ts_pack_play(board_sel_eflag);
        adp_pgNet_start();
    }
    else{
        nstar_ts_pack_play_stop();
        adp_pgNet_stop();
    }
    
}


void nstar_ch_sel(unsigned char channel, unsigned char sta, unsigned char lv)
{
    unsigned char ret=0;
    if(dev_noinit){
       printf("ch do't init\n");
       return; 
    }
    OS_MUTEX_LOCK();
	if(1 == update_trumpet_status(channel, sta, lv))
	{
		ret= ch_sel();
	}
    OS_MUTEX_UNLOCK();
    if(ret){
        encoder_work();
	}
}

void nstar_ch_close_sel(unsigned char channel, unsigned char sta, unsigned char lv)
{
    unsigned char ret=0;
    if(dev_noinit){
       printf("ch do't init\n");
       return; 
    }
    OS_MUTEX_LOCK();
    /*面板的这4种选择是互斥的，先关闭此4种*/
    h_board_ch.level[CH1_NB_MP3].status = 0;
    h_board_ch.level[CH1_NB_MIC].status = 0;
    h_board_ch.level[CH1_NB_LINE1].status = 0;
    h_board_ch.level[CH1_NB_LINE2].status = 0;
    
	if(1 == update_trumpet_status(channel, sta, lv))
	{
		ret= ch_sel();
	}
    OS_MUTEX_UNLOCK();
    
    encoder_work();
	
}










